﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using Domain;

namespace Persistent
{
    public interface IGameRepository : IDisposable
    {
        IQueryable<Game> All { get; }
        Game Find(int id);
        void InsertOrUpdate(Game player);
        void Delete(int id);
        void Save();
    }

    public class GameRepository : BaseRepository, IGameRepository
    {
        public IQueryable<Game> All
        {
            get { return context.Games; }
        }

        public Game Find(int id)
        {
            return context.Games.Find(id)
                    ;
        }

        public void InsertOrUpdate(Game game)
        {
            if (game.GameId == default(int))
            {
                context.Games.Add(game);
            }
            else
            {
                context.Entry(game).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var game = context.Games.Find(id);
            context.Games.Remove(game);
        }
    }
}
